﻿using SmartConference.Api.Applocations.IServices;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;
using System.Transactions;

namespace SmartConference.Api.Applocations.Services
{
    public class RolePermissionServices : IRolePermissionServices
    {
        private readonly IRolePermissionBaseRepository rolePermissionBaseRepository;

        public RolePermissionServices(IRolePermissionBaseRepository rolePermissionBaseRepository)
        {
            this.rolePermissionBaseRepository = rolePermissionBaseRepository;
        }

        public async Task<ApiResult<List<RolePermission>>> GetRolePermission(int RoleId)
        {
            try
            {
                var rolePermission = rolePermissionBaseRepository.GetAll().Where(x => x.RoleId == RoleId).ToList();
                return ApiResult<List<RolePermission>>.Success(ApiResultCode.Success, rolePermission);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult> SetPermission(int RoleId, int[] permissionArray)
        {
            try
            {
                var permissionlist = rolePermissionBaseRepository.GetAll(x => x.RoleId == RoleId);
                using (var tran =new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    if (permissionlist == null)
                    {
                        foreach (var permission in permissionArray)
                        {
                            RolePermission rolePermission = new RolePermission()
                            {
                                RoleId = RoleId,
                                PermissionId = permission
                            };
                            await rolePermissionBaseRepository.AddAsync(rolePermission);
                        }
                        return ApiResult.Success(ApiResultCode.Success);
                    }
                    var permissionlistCount = rolePermissionBaseRepository.GetAll(x => x.RoleId == RoleId).Count();
                    for (var i = 0; i < permissionlistCount; i++)
                    {
                        await rolePermissionBaseRepository.DeleteAsync(await rolePermissionBaseRepository.GetAsync(x => x.RoleId == RoleId));
                    }
                    foreach (var permission in permissionArray)
                    {
                        RolePermission rolePermission = new RolePermission()
                        {
                            RoleId = RoleId,
                            PermissionId = permission
                        };
                        await rolePermissionBaseRepository.AddAsync(rolePermission);
                    }
                    tran.Complete();
                    return ApiResult.Success(ApiResultCode.Success);
                }
                    
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
